package com.apps.sworld.dao;

import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.shelper.ClassHelper;
import com.apps.sutils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.apps.shelper.AnnotationHelper;
import com.apps.saspect.ValidMethod;
import com.apps.sannotation.constraints.NoNull;
import com.apps.sannotation.identity.AccountId;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 账户抽象类
 * @param <T>
 */
public interface sAccount<T> extends sMember<T> {

    default String getAccountIdName()
    {
        return AnnotationHelper.getAnnotationFieldName(AccountId.class,currentModelClass());
    }

    @ValidMethod
    default int countByAccountId(@NoNull Integer account_id)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getAccountIdName(),account_id);
        return count(wrapper);
    }

    @ValidMethod
    default void deleteByAccuntId(@NoNull Integer account_id)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getAccountIdName(),account_id);
        delete(wrapper);
    }
    /**
     * 按资金账户对象的id获取一条记录
     * @param account_id
     * @return
     */
    @ValidMethod
    default T getOneByAccountId(@NoNull Integer account_id)
    {
        return getOneByFieldValue(getAccountIdName(),account_id);
    }

    /**
     * 按资金账户对象的id获取一条记录,没有则创建
     * @param account_id
     * @param t
     * @return
     */
    @ValidMethod
    default T getOneByAccountId(@NoNull Integer account_id,T t)
    {
        T result=getOneByAccountId(account_id);
        if(null == result)
        {
            String filedName=AnnotationHelper.getAnnotationFieldName(AccountId.class,t.getClass());
            if(StringUtils.isEmpty(filedName))
                throw new NullException("账户id不存在或没添加注解进行标识");
            Object filedValue= ClassHelper.getFiledValue(t,filedName);
            if(null == filedValue) {
                ClassHelper.setFiledValue(filedName, t, account_id);
                filedValue=account_id;
            }
            if(!filedValue.equals(account_id))
                throw new DataException("模型的账户id值必须等于传入的账户id");
            return saveToDB(t);
        }
        return result;
    }

    /**
     * 按账户id校验是否为空
     * @param account_id
     * @return
     */
    @ValidMethod
    default boolean isEmptyByAccountId(@NoNull Integer account_id)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getAccountIdName(),account_id);
        return isEmpty(wrapper);
    }

    @ValidMethod
    default boolean isEmptyByAccountId(@NoNull Integer account_id,@NoNull QueryWrapper<T> wrapper)
    {
        wrapper.eq(getAccountIdName(),account_id);
        return isEmpty(wrapper);
    }

    @ValidMethod
    default IPage<T> pageQueryByAccountId(@NoNull Integer account_id, int pageNo, int pageSize)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getAccountIdName(),account_id);
        return pageQuery(wrapper,pageNo,pageSize);
    }

    @ValidMethod
    default IPage<T> pageQueryByAccountId(@NoNull Integer account_id, int pageNo, int pageSize,@NoNull QueryWrapper<T> wrapper)
    {
        wrapper.eq(getAccountIdName(),account_id);
        return pageQuery(wrapper,pageNo,pageSize);
    }


    @ValidMethod
    default List<T> queryByAccountId(@NoNull Integer account_id)
    {
        return queryByFieldValue(getAccountIdName(),account_id);
    }

    @ValidMethod
    default List<T> queryByAccountId(@NoNull Integer account_id,@NoNull QueryWrapper<T> wrapper)
    {
        wrapper.eq(getAccountIdName(),account_id);
        return query(wrapper);
    }

    /**
     * 按时间区段分页查询
     * @param account_id
     * @param start
     * @param end
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ValidMethod
    default IPage<T> pageQueryTimeIntervalByAccountId(@NoNull Integer account_id, @NoNull LocalDateTime start, @NoNull LocalDateTime end, int pageNo, int pageSize)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getAccountIdName(),account_id);
        return pageQueryTimeInterval(wrapper,start,end,pageNo,pageSize);
    }

    /**
     * 按时间区间查询
     * @param account_id
     * @param start
     * @param end
     * @return
     */
    @ValidMethod
    default List<T> queryTimeIntervalByAccountId(@NoNull Integer account_id, @NoNull LocalDateTime start, @NoNull LocalDateTime end)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getAccountIdName(),account_id);
        return queryTimeInterval(wrapper,start,end);
    }

    @ValidMethod
    default T updateOneFieldByAccountId(@NoNull Integer account_id,@NoNull String fieldname,@NoNull Object value)
    {
        T t=getOneByAccountId(account_id);
        if(null == t)
            throw new NullException("account_id为"+account_id+"的记录不存在");
        ClassHelper.setFiledValue(fieldname,t,value);
        return updateById(t);
    }

}
